package com.lambda.hybrid;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

public class HybridConfig {
    private static final String BACKGROUND_COLOR = "backgroundColor";

    private static final String HIDE_STATUS_BAR = "hideStatusBar";
    private static final String STATUS_BAR_COLOR = "statusBarColor";

    private static final String HIDE_NAV_BAR = "hideNavBar";
    private static final String NAV_BAR_COLOR = "navBarBackgroundColor";

    private static final String USE_WEB_TITLE = "useWebTitle";
    private static final String TITLE = "title";
    private static final String TITLE_COLOR = "titleColor";

    private static final String HIDE_BACK_BUTTON = "hideBackButton";
    private static final String BACK_BUTTON_TITLE = "backButtonTitle";
    private static final String BACK_BUTTON_IMAGE = "backButtonImage";

    private static final String HIDE_CLOSE_BUTTON = "enableCloseButton";
    private static final String CLOSE_BUTTON_TITLE = "closeButtonTitle";
    private static final String CLOSE_BUTTON_IMAGE = "closeButtonImage";

    private static final String PROGRESS_ENABLE = "enableProgress";
    private static final String PROGRESS_COLOR = "progressColor";
    private static final String PROGRESS_HEIGHT = "progressHeight";

    private final HybridView mHybridView;
    private final JSONObject mConfig;

    public HybridConfig(HybridView hybridView) {
        mHybridView = hybridView;

        mConfig = new JSONObject();
        try {
            mConfig.put(BACKGROUND_COLOR, 0xFFFFFF98);

            mConfig.put(HIDE_STATUS_BAR, false);
            mConfig.put(STATUS_BAR_COLOR, 0x6F000000);

            mConfig.put(HIDE_NAV_BAR, false);
            mConfig.put(NAV_BAR_COLOR, Color.WHITE);

            mConfig.put(USE_WEB_TITLE, true);
            mConfig.put(TITLE, null);
            mConfig.put(TITLE_COLOR, 0x333333);

            mConfig.put(HIDE_BACK_BUTTON, false);
            mConfig.put(BACK_BUTTON_TITLE, null);
            mConfig.put(BACK_BUTTON_IMAGE, "ic_back");

            mConfig.put(HIDE_CLOSE_BUTTON, false);
            mConfig.put(CLOSE_BUTTON_TITLE, null);
            mConfig.put(CLOSE_BUTTON_IMAGE, "ic_close");

            mConfig.put(PROGRESS_ENABLE, true);
            mConfig.put(PROGRESS_COLOR, 0xFF1E90FF);
            mConfig.put(PROGRESS_HEIGHT, 3);
        } catch (Exception e) {/**/}
    }

    public boolean isProgressEnable() {
        boolean result = false;
        try {
            result = mConfig.getBoolean(PROGRESS_ENABLE);
        } catch (JSONException e) {/**/}
        return result;
    }

    public int getProgressHeight() {
        int result = 3;
        try {
            result = mConfig.getInt(PROGRESS_HEIGHT);
        } catch (JSONException e) {/**/}
        return result;
    }

    public int getProgressColor() {
        int result = 3;
        try {
            result = mConfig.getInt(PROGRESS_COLOR);
        } catch (JSONException e) {/**/}
        return result;
    }

    public void update(String data) throws JSONException {
        JSONObject newConfig = new JSONObject(data);
        Iterator<String> iterator = newConfig.keys();
        while (iterator.hasNext()) {
            String key = iterator.next();
            mConfig.put(key, newConfig.get(key));

            switch (key) {
                case BACKGROUND_COLOR:
                    mHybridView.setBackgroundColor(mConfig.getInt(key));
                    break;

                case HIDE_STATUS_BAR:
                    mHybridView.statusBar.setVisibility(mConfig.getBoolean(key) ? View.GONE : View.VISIBLE);
                    break;
                case STATUS_BAR_COLOR:
                    mHybridView.statusBar.setBackgroundColor(mConfig.getInt(key));
                    break;

                case HIDE_NAV_BAR:
                    mHybridView.toolbar.setVisible(mConfig.getBoolean(key));
                    break;
                case NAV_BAR_COLOR:
                    mHybridView.toolbar.setBackgroundColor(mConfig.getInt(key));
                    break;

                case USE_WEB_TITLE:
                    String title = mHybridView.getTitle();
                    if (!TextUtils.isEmpty(title)) {
                        mHybridView.toolbar.setTitle(title);
                    }
                    break;
                case TITLE:
                    mHybridView.toolbar.setTitle(mConfig.getString(key));
                    break;
                case TITLE_COLOR:
                    mHybridView.toolbar.setTitleColor(mConfig.getInt(key));
                    break;

                case HIDE_BACK_BUTTON:
                    mHybridView.toolbar.hideBackButton(mConfig.getBoolean(key));
                    break;
                case BACK_BUTTON_TITLE:
                    mHybridView.toolbar.setBackButtonTitle(mConfig.getString(key));
                    break;
                case BACK_BUTTON_IMAGE:
                    mHybridView.toolbar.setBackButtonImage(mConfig.getString(key));
                    break;

                case HIDE_CLOSE_BUTTON:
                    mHybridView.toolbar.hideCloseButton(mConfig.getBoolean(key));
                    break;
                case CLOSE_BUTTON_TITLE:
                    mHybridView.toolbar.setCloseButtonTitle(mConfig.getString(key));
                    break;
                case CLOSE_BUTTON_IMAGE:
                    mHybridView.toolbar.setCloseButtonImage(mConfig.getString(key));
                    break;
                default:
                    throw new JSONException("unknown property(: " + key + ")");
            }
        }
    }

    public void onPageFinished() {
        mHybridView.toolbar.hideCloseButton(!mHybridView.canGoBack());

        String title = null;
        if (mConfig.optBoolean(USE_WEB_TITLE, true)) {
            title = mHybridView.getTitle();
        } else {
            try {
                title = mConfig.getString(TITLE);
            } catch (Exception e) {/**/}
        }

        if (!TextUtils.isEmpty(title)) {
            mHybridView.toolbar.setTitle(title);
        }
    }
}
